<?php

/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/10/10
 * Time: 10:09
 */

namespace app\api\controller;


use app\api\BaseController;
use app\common\model\Member;
use app\common\model\Token;
use think\Lang;
use think\Request;
use think\Db;

/**
 * @title 用户
 * @description 用户相关
 */
class User extends BaseController
{

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        Lang::load(APP_PATH . 'home/lang/' . config('default_lang') . '/login.lang.php');
    }

    /**
     * @title 登录
     * @description
     * @param string member_name 名称 '' true
     * @param string member_password 年龄 '' true
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /user/login
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function login()
    {
        $member_model = new Member();
        $token_model = new Token();
        $data = array(
            'member_name' => input('post.member_name'),
            'member_password' => input('post.member_password'),
        );
        $login_validate = validate('member');
        if (!$login_validate->scene('login')->check($data)) {
            ds_json_encode(10001, $login_validate->getError());
        }
        $map = array(
            'member_name' => $data['member_name'],
            'member_password' => md5($data['member_password']),
        );
        $member_ = db('member')->where(['member_name' => $data['member_name']])->find();
        if (!$member_) {
            return json_fail_return('该用户不存在');
        }
        $member_info = $member_model->getMemberInfo($map);
        if (empty($member_info)) {
            return json_fail_return('账号密码错误');
        }
        $member_info['customer_name'] = empty($member_info['customer_name']) ? '未设置' : $member_info['customer_name'];
        $member_info['member_type'] = getUserIdentity($member_info['member_type']);
        if (empty($member_info) && preg_match('/^0?(13|15|17|18|14)[0-9]{9}$/i', $data['member_name'])) {
            //根据会员名没找到时查手机号
            $map = array();
            $map['member_mobile'] = $data['member_name'];
            $map['member_password'] = md5($data['member_password']);
            $member_info = db('member')->where($map)->find();
        }
        if (empty($member_info) && (strpos($data['member_name'], '@') > 0)) {
            //按邮箱和密码查询会员
            $map = array();
            $map['member_email'] = $data['member_name'];
            $map['member_password'] = md5($data['member_password']);
            $member_info = db('member')->where($map)->find();
        }

        if ($member_info) {
            if (!$member_info['member_state']) {
                return json_fail_return('请联系后台管理员开通权限');
            }

            $token = $this->buildAccessToken();

            $token_info = $token_model->getToken(['user_id' => $member_info['member_id']]);
            if ($token_info) {
                $token_model->updateById(['token' => $token, 'expire_time' => time() + 24 * 60 * 60 * 10, 'updated_at' => date('Y-m-d H:i:s')], $token_info['id']);
            } else {
                $add_data = array(
                    'user_id' => $member_info['member_id'],
                    'token' => $token,
                    'expire_time' => time() + 24 * 60 * 60 * 10,
                    'created_at' => date('Y-m-d H:i:s')
                );
                if (!$token_model->addToken($add_data)) {
                    return json_fail_return('token生成失败,请重试');
                }
            }
            $login_ip = request()->ip();

            //修改最后登录时间和增加登录次数
            $update_member_info['member_loginnum'] = $member_info['member_loginnum'] + 1;
            $update_member_info['member_logintime'] = time();
            $update_member_info['member_login_ip'] = $login_ip;
            $update_member_info['member_login_ip'] = $login_ip;
            $update_member_info['clientid'] = input('clientid');
            db('member')->where(['member_id' => $member_info['member_id']])->update($update_member_info);

            //判断当前是否是新用户 送优惠劵 送积分
            if ($member_info['member_loginnum'] == 0) {
                #积分
                //如果是客户邀请客户的话，新客户到默认运营商下面，但是积分送给上级邀请人
                if ($member_info['member_type'] == 0 && $member_info['invite_type'] == 0) {
                    #通过上级邀请人的id，送积分
                    $superior_info = db('member')->where(['member_id' => $member_info['inviter_id']])->find();
                    model('points')->savePointslog('inviter', array(
                        'pl_memberid' => $superior_info['member_id'],
                        'pl_membername' => $superior_info['member_name'],
                        'invited' => $member_info['member_name']
                    ), false);
                }
            }


            $result = array(
                'token' => $token,
                'member_info' => $member_info
            );

            return json_success_return($result, '登录成功');
        } else {
            return json_fail_return('账号或密码错误');
        }
    }

     /**
     * @title 登陆查询优惠券
     * @description
     * @param string token 
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /user/login_voucher
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function login_voucher()
    {
        $member_id = $this->user_id;
        $userInfo = $this->user_info;
        $firstLoginVoucher = [];
        if($userInfo['member_loginnum'] == 1){//第一次登陆的优惠券
            $firstLoginVoucher = db('voucher_type')->where(['type_status' => 1, 'is_del' => 0, 'pattern_id' => 1])->field('voucher_id')->select();
        }

        //定时发放的优惠券
        $timeVouchers = db('voucher_type')
            ->where(['type_status' => 1, 'is_del' => 0, 'pattern_id' => 2])
            ->where('timing_start_time', '<', date('Y-m-d H:i:s'))
            ->where('timing_end_time', '>', date('Y-m-d H:i:s'))
            ->field('voucher_id')
            ->select();
        if($timeVouchers){
            foreach($timeVouchers as $key => $val){
                $voucherId = $val['voucher_id'];
                $temp = db('voucher_collection')
                    ->where('member_id', $member_id)
                    ->where('voucher_id', $voucherId)
                    ->where('is_send_del', 0)
                    ->find();
                if($temp){
                    unset($timeVouchers[$key]);
                }    
            }
        }

        $vouchers = array_merge($timeVouchers, $firstLoginVoucher);
        if($vouchers){
            $voucherIds = [];
            foreach($vouchers as $key=> $val){
                $voucherIds[] = $val['voucher_id'];
            }

            $voucherList = db('voucher')->where('voucher_id', 'in', $voucherIds)->field('voucher_id,voucher_title,voucher_price,voucher_limit')->select();

            return json_success_return($voucherList);
        }

        return json_success_return([]);
    }

    public function login_send_voucher(Request $request)
    {
        $member_id = $this->user_id;
        $voucherIds = $request->post('voucher_id/a');
        if (!$voucherIds) {
            return json_fail_return('优惠券ID不能为空');
        }
        foreach($voucherIds as $voucherId){
            $voucher_place_info = db('voucher')->where(['voucher_id' => $voucherId])->find();
            $voucher_data = [
                'member_id' => $member_id,
                'voucher_type_id' => 1,
                'voucher_id' => $voucherId,
                'voucher_name' => $voucher_place_info['voucher_title'],
                'due_time' => $voucher_place_info['voucher_enddate'],
                'voucher_collection_price' => $voucher_place_info['voucher_price'],
                'is_used' => 1,
                'create_time' => time()
            ];
            db('voucher_collection')->insert($voucher_data);

           #减数量
            $surplus_count = intval($voucher_place_info['surplus_count'])-1;
            db('voucher')->where(['voucher_id' => $voucherId])->data(['surplus_count'=>$surplus_count])->update();

        }
        
        return json_success_return([], '优惠券发送成功');
    }

    /**
     * @title 注册
     * @description
     * @param string member_name 账号 '' true
     * @param string member_password 密码 '' true
     * @param string member_password_confirm 重复密码 '' true
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /user/register
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function register()
    {
        $memberModel = new Member();
        if (input('post.member_password') != input('post.member_password_confirm')) {
            return json_fail_return(lang('login_passwords_not_match'));
        }
        $data = array(
            'member_name' => input('post.member_name'),
            'member_password' => input('post.member_password'),
            'member_password_confirm' => input('post.member_password_confirm'),
            'invite_code' => input('post.invite_code', ''),
            'member_mobile' => input('post.member_name'),
        );
        $login_validate = validate('member');
        if (!$login_validate->scene('register')->check($data)) {
            return json_fail_return($login_validate->getError());
        }
        $post = [
            'username' => 'kehu' . $data['member_name'],
            'password' => $data['member_password'],
            'nickname' => $data['member_name'],
            'avatar' => 'http://33wang.douyinhot.com/cfe45202004231527382825.png',
            'signature' => '',
        ];

        JmRegist($post);
        $member_info = $memberModel->register($data);
        if (!isset($member_info['error'])) {
            return json_success_return([], '注册成功');
        } else {
            return json_fail_return($member_info['error']);
        }
    }

    /**
     * @title 修改密码
     * @description
     * @param string member_password 原密码 '' true
     * @param string member_password_new 新密码 '' true
     * @param string member_password_confirm 重复密码 '' true
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /user/updatePwd
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function updatePwd()
    {
        $password = input('post.password');
        $password_confirm = input('post.newtwopassword');
        $password_new = input('post.newpassword');
        if (md5($password) != $this->user_info['member_password']) {
            return json_fail_return('密码错误');
        }
        if ($password_new != $password_confirm) {
            return json_fail_return('两次密码不一致');
        }
        $re = db('member')->where(['member_id' => $this->user_id])->update(['member_password' => md5($password_new)]);
        if ($re) {
            return json_success_return([], '修改成功');
        } else {
            return json_fail_return('修改失败');
        }
    }

    /**
     * @title 获取用户信息
     * @description
     * @param string none
     *
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /user/userInfo
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function userInfo()
    {
        return json_success_return('success', $this->user_info);
    }

    /**
     * @title 修改信息
     * @description
     * @param string member_nickname 昵称
     * @param string member_sex 性别
     * @param string member_qq qq
     * @param string province_id 省
     * @param string city_id 市
     * @param string area_id 详细地址
     * @param string birthday 详细地址
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /user/upInfo
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function upInfo()
    {
        $member_model = new Member();
        $member_array = array();
        $member_array['member_nickname'] = input('post.member_nickname');
        $member_array['member_sex'] = input('post.member_sex');
        $member_array['member_qq'] = input('post.member_qq');
        $member_array['member_areaid'] = input('post.area_id');
        $member_array['member_cityid'] = input('post.city_id');
        $member_array['member_provinceid'] = input('post.province_id');
        $member_array['member_areainfo'] = input('post.area_info');
        if (strlen(input('post.birthday')) == 10) {
            $member_array['member_birthday'] = strtotime(input('post.birthday'));
        }
        $update = $member_model->editMember(array('member_id' => $this->user_id), $member_array);

        $message = $update ? lang('ds_common_save_succ') : lang('ds_common_save_fail');

        if ($update) {
            return json_success_return($message);
        } else {
            return json_fail_return($message);
        }
    }

    /**
     * @title 修改头像
     * @description
     * @param file pic 文件
     * @return {"code":200,"msg":"succeess"}
     * @author fuzz
     * @url /user/changeAvat
     * @method POST
     *
     * @code 200 成功
     * @code 201 失败
     *
     */
    public function changeAvat()
    {
        $member_id = $this->user_id;
        if (!empty($_FILES['pic']['tmp_name'])) {
            $file_object = request()->file('pic');
            $base_url = BASE_UPLOAD_PATH . '/' . ATTACH_AVATAR . '/';
            $ext = strtolower(pathinfo($_FILES['pic']['name'], PATHINFO_EXTENSION));
            $file_name = 'avatar_' . $member_id . '_new' . ".$ext";
            $info = $file_object->rule('uniqid')->validate(['ext' => ALLOW_IMG_EXT])->move($base_url, $file_name);
            if ($info) {
                $name_dir = BASE_UPLOAD_PATH . '/' . ATTACH_AVATAR . '/' . $info->getFilename();

                $imageinfo = getimagesize($name_dir);
                return json_success_return($imageinfo);
            } else {
                return json_fail_return($file_object->getError());
            }
        } else {
            return json_fail_return(lang('upload_failed_replace_pictures'));
        }
    }


    //我的优惠券
    public function discount()
    {
        $voucher_owner_id = $this->user_id;
        $tblname = 'voucher_collection';
        $where['a.member_id'] = $voucher_owner_id;
        $where['b.status'] = 1;
        $where['a.is_used'] = 1;
        $field = 'a.id,a.voucher_type_id,a.voucher_name,a.is_used,a.used_time,a.create_time,b.voucher_price,b.vou_gcname,b.voucher_startdate,b.voucher_enddate,b.vou_type,a.due_time,b.voucher_limit';
        $invoice_info = db($tblname . ' a')->join('__VOUCHER__ b', 'a.voucher_id=b.voucher_id')->where($where)->field($field)->order('a.id desc')->select();

        return json_success_return($invoice_info);
    }

    /**
     * Notes:兑换优惠券
     * User: shen
     * Date: 2020/4/22
     * Time: 17:47
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function score_exchange_coupon()
    {
        $user_id = $this->user_id;

        $voucher_id = input('voucher_id/d');
        if (!$voucher_id) {
            return json_fail_return('缺少优惠券id');
        }
        $tblname = 'voucher_collection';
        $where['member_id'] = $user_id;
        $where['is_used'] = 1;
        $where['voucher_id'] = $voucher_id;
        $invoice_info = db($tblname)->where($where)->find();

        if ($invoice_info) {
            return json_fail_return('已拥有该优惠券，不能重复兑换');
        }
        $wheres['voucher_id'] = array('eq', $voucher_id);
        $wheres['voucher_enddate'] = array('>', time());
        $wheres['voucher_startdate'] = array('<', time());
        $wheres['count'] = array('>', 0);
        $wheres['is_del'] = array('eq', 0);
        $wheres['status'] = array('eq', 1);
        // p($wheres);die;
        $voucher_info = db('voucher')->where($wheres)->find();
        if (!$voucher_info) {
            return json_fail_return('该优惠券不存在');
        }

        $member_info = db('member')->where('member_id', $user_id)->find();
        if ($member_info['member_points'] < $voucher_info['score_count']) {
            return json_fail_return('积分不够');
        }

        $new_score = $member_info['member_points'] - $voucher_info['score_count'];

        #修改用户积分
        db('member')->where('member_id', $user_id)->data('member_points', $new_score)->update();

        $new_count = $voucher_info['count'] - 1;
        #修改优惠券数量
        db('voucher')->where('voucher_id', $voucher_id)->data('count', $new_count)->update();

        #添加使用积分记录
        $insert_log_data = [
            'pl_membername' => $member_info['member_name'],
            'pl_memberid' => $member_info['member_id'],
            'pl_points' => -$voucher_info['score_count'],
            'pl_addtime' => time(),
            'pl_desc' => '兑换优惠券',
            'pl_stage' => 'coupon',
            'pl_balance' => $new_score
        ];

        db('pointslog')->data($insert_log_data)->insert();

        $due_time = time() + $voucher_info['use_date'] * 86400;
        #添加优惠券
        $insert_coupon_data = [
            'member_id' => $user_id,
            'voucher_type_id' => 4,
            'voucher_id' => $voucher_id,
            'voucher_name' => $voucher_info['voucher_title'],
            'is_used' => 1,
            'create_time' => time(),
            'is_send_del' => 0,
            'due_time' => $due_time,
            'voucher_collection_price' => $voucher_info['voucher_price'],
        ];

        db('voucher_collection')->data($insert_coupon_data)->insert();


        return json_success_return('兑换成功');
    }


    public function inspect_app_edition()
    {
        $type = input('type', 1);  #1.客户端 2.服务端

        if ($type == 1) {
            $config_list = db('config')->where('code', 'in', 'user_app_edition,user_app_down_url')->select();
        } else {
            $config_list = db('config')->where('code', 'in', 'server_app_edition,server_app_down_url')->select();
        }

        $data = [];
        $data['app_edition'] = $config_list['0']['value'];
        $data['down_url'] = $config_list['1']['value'];

        return json_success_return($data, '操作成功');
    }

    /**
     * 意见反馈
     */
    public function feedback()
    {
        if (!(input('content'))) {
            json_fail_return('请正确输入信息');
        }
        $userid = $this->user_id;
        $user_info = $this->user_info;

        $data = [
            'mbfb_content' => input('content'),
            'mbfb_type' => 4, //服务端
            'mbfb_time' => time(),
            'mbfb_image' => input('image') ? input('image') : '',
            'member_id' => $userid,
            'member_name' => $user_info['member_name'],
        ];
        $model2 = model('mbfeedback');
        $bool = $model2->addMbfeedback($data);
        if ($bool) {
            return json_success_return([], '反馈成功');
        } else {
            return json_success_return('反馈失败');
        }
    }

    #个人中心--我的钱包
    public function getMyWallet()
    {
        #获取积分
        $integral = db('member')->where(['member_id' => $this->user_id])->find();
        $result['member_points'] = $integral['member_points'];
        return json_success_return($result);
    }

    #个人中心--积分记录
    public function getMyWalletLog()
    {
        #获取积分
        $result = db('pointslog')->where(['pl_memberid' => $this->user_id])->order('pl_addtime desc')->select();

        return json_success_return($result);
    }

    #个人中心--分享
    public function getMyCode()
    {
        #获取积分
        $member_info = db('member')->where(['member_id' => $this->user_id])->find();

        json_success_return(['code' => $member_info['invite_code'], 'img_code' => $member_info['kehu_code'], 'kehu_img_code' => $member_info['kehu_img_code']], '请求成功');
    }

    #个人中心--我的账号
    public function getMyAccount()
    {
        $member_info = [];
        $member_info = db('member')
            ->field('invite_parent_code,member_id,member_name,member_address,member_points,member_avatar,member_nickname,member_qq,member_wx,invite_store_name,enterprise_name,member_mobile,member_type,invite_type,enterprise_name')
            ->where(['member_id' => $this->user_id])
            ->find();
        if (empty($member_info)) {
            return json_fail_return('此用户不存在');
        }

        if ($member_info['invite_parent_code']) {
            $parent_member_info = db('member')->where(['invite_code' => $member_info['invite_parent_code']])->find();
            if ($parent_member_info) {
                if ($parent_member_info['member_type'] == 8) {
                    $member_info['enterprise_name'] = $parent_member_info['enterprise_name'];
                } else {
                    $member_info['enterprise_name'] = '';
                }
            } else {
                $member_info['enterprise_name'] = '';
            }
        } else {
            $member_info['enterprise_name'] = '';
        }

        $config = db('config')->where('code', 'qiniu_http')->find();

        if (empty($member_info['member_nickname'])) {
            $member_info['member_nickname'] = '未设置';
        }
        if ($member_info['member_avatar'] === null) {
            $member_info['member_avatar'] = 'http://33wang.douyinhot.com/46f5b202003131520417317.png';
        } else {
            $member_info['member_avatar'] = $config['value'] . '/' . $member_info['member_avatar'];
        }
        if ($member_info['member_qq'] === null) {
            $member_info['member_qq'] = '未设置';
        }
        json_success_return($member_info);
    }

    #个人中心--修改 我的账号
    public function updateMyAccount()
    {
        $member_type = input('post.member_type');  # 1--修改头像 2--修改昵称 3--修改微信号 4--修改QQ号
        switch ($member_type) {
            case 1:
                $avatar = explode('/', input('post.member_avatar'));
                $update_data['member_avatar'] = $avatar[3];
                break;
            case 2:
                $update_data['member_nickname'] = input('post.member_nickname');
                break;
            case 3:
                $update_data['member_wx'] = input('post.member_wx');
                break;
            case 5:
                $update_data['member_address'] = input('post.member_address');
                $update_data['province_id'] = input('post.province_id');
                $update_data['city_id'] = input('post.city_id');
                $update_data['area_id'] = input('post.area_id');
                break;
            default:
                $update_data['member_qq'] = input('post.member_qq');
                break;
        }
        db('member')->where(['member_id' => $this->user_id])->update($update_data);
        if($member_type ==1){
            $member_info = db('member')->where(['member_id' => $this->user_id])->find();
            $file_data['file_name'] = input('post.member_avatar');

           #极光更新头像
           updateUserAvtar($file_data,'kehu'.$member_info['member_name']);
        }

        json_success_return([], '修改成功');
    }

    #个人中心--浏览记录
    public function browseRecord()
    {
        $result = db('record')->where(['member_id' => $this->user_id])->order('add_time desc')->select();
        $shop = [];

        $project = [];

        foreach ($result as $k => $v) {

            if ($v['type'] == 1) {

                $shop[] = $v;
            } else {

                $project[] = $v;
            }
        }

        $result = [

            'shop' => $shop,

            'project' => $project

        ];

        json_success_return($result);
    }

    #客户端 公告列表
    public function getNoticeList()
    {
        $time = time();
        $page = intval(input('page')) ? intval(input('page')) : 1;
        $pagesize = intval(input('pagesize')) ? intval(input('pagesize')) : 15;
        $typeid = input('typeid');
        $where = [];
        if ($typeid) {
            $where['typeid'] = $typeid;
        }
        $offset = ($page - 1) * $pagesize;
        #当前大于等于开始时间,当前时间小于等于结果时间
        $result = Db::name('affiches')
            ->where('start_time', '<=', $time)
            ->where('end_time', '>=', $time)
            ->where($where)
            ->order('sort desc')
            ->limit($offset, $pagesize)
            ->field('affiche_id,title,updated_at')
            ->select();

        $user_id = $this->user_id;
        foreach ($result as $key => $value) {
            $read_info = db('affiche_reads')->where(['affiche_id' => $value['affiche_id'], 'member_id' => $user_id])->select();
            if (!empty($read_info)) {
                $result[$key]['read_status'] = 1; #已读
            } else {
                $result[$key]['read_status'] = 0; #未读
            }
        }
        return json_success_return($result);
    }

    #客户端 公告详情
    public function getNoticeDetail()
    {
        $time = time();
        if (empty($id = intval(input('detailid')))) {
            return json_fail_return('请正确输入信息');
        }
        $where['affiche_id'] = $id;
        $result = Db::name('affiches')
            ->where('start_time', '<=', $time)
            ->where('end_time', '>=', $time)
            ->where($where)
            ->field('affiche_id,title,contents,updated_at')
            ->select();

        #判断已读未读
        $user_id = $this->user_id;

        $read_info = db('affiche_reads')->where(['affiche_id' => $id, 'member_id' => $user_id])->select();
        $affiche_info = db('affiches')->where(['affiche_id' => $id])->find();

        if (empty($read_info)) {
            $data = [
                'member_id' => $user_id,
                'affiche_id' => $id,
                'created_at' => time()
            ];
            db('affiche_reads')->insertGetId($data);

            $new['already_read'] = $affiche_info['already_read'] + 1;
            $new['unread'] = $affiche_info['unread'] - 1;
            db('affiches')->where(['affiche_id' => $id])->update($new);
        }

        return json_success_return($result);
    }

    //获取资讯分类
    public function getInformationClass()
    {

        $informationclass = db('informationclass')->where(['ac_parent_id' => 0])->select();

        json_success_return($informationclass);
    }

    //资讯列表
    public function getInformationList()
    {
        $data = input('post.');
        $ac_id = intval(input('ac_id'));
        $type = intval(input('type')); #0客户端 1服务端

        $where = [
            'ac_id' => $ac_id,
            'article_show' => 1,
            'ac_address' => ['in',"{$type},2"]
        ];
        if (!empty($data['information_title'])) {
            $where['article_title'] = array('like', '%' . $data['information_title'] . '%');
        }
        $page = intval(input('page')) ? intval(input('page')) : 1;
        $pagesize = intval(input('pagesize')) ? intval(input('pagesize')) : 15;
        $offset = ($page - 1) * $pagesize;

        $config = db('config')->where('code', 'qiniu_http')->find();
        $informationclass = [];
        $informationclass = db('information')
            ->where($where)
            ->order('article_thumbs', 'desc')
            ->limit($offset, $pagesize)
            ->select();
        if (!empty($informationclass)) {
            foreach ($informationclass as $key => $value) {
                $informationclass[$key]['article_pic'] = $config['value'] . '/' . $value['article_pic'];
                $informationclass[$key]['article_time'] = date('Y-m-d H:i:s', $value['article_time']);
                $article_content = htmlspecialchars_decode($value['article_content']);
                $new_content = strip_tags($article_content);
                $informationclass[$key]['article_content'] = $new_content;
                //需要判断当前用户是否点赞
                $parms = [
                    'member_id' => $this->user_id,
                    'article_id' => $value['article_id']
                ];
                $thumbs_info = db('information_thumbs')->where($parms)->find();
                $informationclass[$key]['is_thumbs'] = 1; #0--已点赞 1--未点赞 
                if (!empty($thumbs_info) && $thumbs_info['status'] == 0) {
                    $informationclass[$key]['is_thumbs'] = 0;
                }
            }
        }
        json_success_return($informationclass);
    }

    //资讯详情
    public function getInformationDetail()
    {
        $article_id = intval(input('article_id'));
        $where = [
            'article_id' => $article_id,
        ];
        $information = db('information')->where($where)->find();
        $config = db('config')->where('code', 'qiniu_http')->find();
        $information['article_pic'] = $config['value'] . '/' . $information['article_pic'];
        $information['article_time'] = date('Y-m-d H:i:s', $information['article_time']);
        $article_content = htmlspecialchars_decode($information['article_content']);
        $new_content = strip_tags($article_content);
        $information['article_content'] = $new_content;

        #阅读量 +1
        $new_information['article_click'] = $information['article_click'] + 1;
        db('information')->where($where)->update($new_information);

        $parms = [
            'article_id' => $article_id,
            'member_id' => $this->user_id
        ];
        $thumbs_info = db('information_thumbs')->where($parms)->find();

        $information['is_thumbs'] = 1; #0--已点赞 1--未点赞 
        if (!empty($thumbs_info) && $thumbs_info['status'] == 0) {
            $information['is_thumbs'] = 0;
        }
        json_success_return($information);
    }

    //资讯点赞
    public function getInformationThumbs()
    {
        $article_id = intval(input('article_id'));
        $where = [
            'article_id' => $article_id,
        ];
        $information = db('information')->where($where)->find();

        #插入点赞数据
        #判断是否有点赞数据
        $parms = [
            'article_id' => $article_id,
            'member_id' => $this->user_id,
            'type' => 1
        ];
        $thumbs_info = db('information_thumbs')->where($parms)->find();

        if (empty($thumbs_info)) {
            db('information_thumbs')->insertGetId($parms);
            #点赞量 +1
            $new_information['article_thumbs'] = $information['article_thumbs'] + 1;
        } else {
            if ($thumbs_info['status'] == 0) {
                db('information_thumbs')->where(['id' => $thumbs_info['id']])->update(['status' => 1]);
                #点赞量 -1
                $new_information['article_thumbs'] = $information['article_thumbs'] - 1;
            } else {
                db('information_thumbs')->where(['id' => $thumbs_info['id']])->update(['status' => 0]);
                #点赞量 +1
                $new_information['article_thumbs'] = $information['article_thumbs'] + 1;
            }
        }

        //更新点赞数量
        db('information')->where($where)->update($new_information);
        json_success_return([], '点赞成功');
    }

    //我的设备列表
    public function getMyDevice()
    {
        $data = input('post.');
        $parms = [
            'order_type' => array('in', '1,2,3,4'),
            'service_type' => 0,
            'so.member_id' => $this->user_id,
            'order_state' => 4 #已完成
        ];

        if (!empty($data['goods_name'])) {
            $parms['sod.goods_name'] = array('like', '%' . $data['goods_name'] . '%');
        }

        $page = intval(input('page')) ? intval(input('page')) : 1;
        $pagesize = intval(input('pagesize')) ? intval(input('pagesize')) : 15;
        $offset = ($page - 1) * $pagesize;

        $field = 'sod.id,goods_id,sod.goods_name,b_name,sod.type_model,next_care_time,ne.equipment_images';

        $order_list = db('s_order')->alias('so')
            ->join('s_order_detail sod', 'sod.order_id =so.order_id')
            ->join('new_equipment_brand neb', 'neb.equipment_brand_id =sod.goods_id')
            ->join('new_equipment ne', 'ne.id =neb.equipment_id')
            ->field($field)
            ->limit($offset, $pagesize)
            ->where($parms)
            ->select();

        $config = db('config')->where('code', 'qiniu_http')->find();

        foreach ($order_list as $k => $v) {
            $order_list[$k]['equipment_images'] = $config['value'] . '/' . $v['equipment_images'];
        }
        json_success_return($order_list);
    }

    //设备维修记录
    public function getMyDeviceType()
    {
        $device_id = input('post.device_id');
        $type = input('post.type');

        $page = intval(input('page')) ? intval(input('page')) : 1;
        $pagesize = intval(input('pagesize')) ? intval(input('pagesize')) : 15;
        $offset = ($page - 1) * $pagesize;

        $goods_detail_info = db('s_order_detail')->alias('sod')
            ->join('s_order s', 's.order_id = sod.order_id')
            ->limit($offset, $pagesize)
            ->where(['id' => $device_id, 'order_type' => $type])
            ->find();
        $goods_detail_info['option_info'] = json_decode($goods_detail_info['option_info'], true);

        $option = [];
        $option_info = '';
        $device_list = [];
        if (!empty($goods_detail_info['option_info'])) {
            foreach ($goods_detail_info['option_info'] as $k => $v) {
                if ($v['selected'] == 1) {
                    $option[] = $v['content'];
                }
            }
            $option_info = implode(',', $option);
            $device_list[] = [
                'order_id' => $goods_detail_info['order_id'],
                'order_type' => $goods_detail_info['order_type'],
                'add_time' => date('Y-m-d H:i:s', $goods_detail_info['add_time']),
                // 'option_info' => $option_info
                'option_info' => $goods_detail_info['goods_name']
            ];
        }

        json_success_return($device_list);
    }


    /**
     * Notes: 首页获取前三天预约服务订单提示
     * User: shen
     * Date: 2020/4/24
     * Time: 9:46
     */
    public function service_tips()
    {

        $token = input('token', '');
        if ($token) {
            $token_info = db('token')->where('token', $token)->find();
            if ($token_info) {
                $three_days_ago = time() - 3 * 86400;
                $where = [];
                $where['order_state'] = array('eq', 7);
                $where['member_id'] = array('eq', $token_info['user_id']);
                $where['appointment_unix_time'] = array('between', [$three_days_ago, time()]);
                $s_order_info = db('s_order')->where($where)->select();
                if ($s_order_info) {
                    $return_data = [];

                    foreach ($s_order_info as $k => $v) {
                        $return_data[$k]['s_order_id'] = $v['order_id'];
                        $return_data[$k]['time'] = date('m月d号H点', $v['appointment_unix_time']);
                        $return_data[$k]['text'] = '您' . $return_data[$k]['time'] . '有预约服务，请等待工作人员上门吧！';
                    }
                    json_success_return($return_data, '操作成功');
                } else {
                    json_success_return([], '操作成功');
                }
            } else {
                json_success_return([], '操作成功');
            }
        } else {
            json_success_return([], '操作成功');
        }
    }
 /**
     * @name: xiaozou
     * @title: 两端获取消息推送信息
     * @url: 
     * @param {type} 
     * @return: 
     */
    public function get_affiches_order()
    {
        $token = input('token', '');
        $token_info = db('token')->where('token', $token)->find();

        $parms = [
            'notif_type' => ['in', '0,2'],
            'member_id' => $token_info['user_id'],
            'member_type' => 0,
        ];
        $affiche_list = db('affiches_order')->where($parms)->select();
        foreach ($affiche_list as $k => $v) {
            $affiche_list[$k]['c_time'] = date('Y-m-d H:i:s', $v['c_time']);
        }
        json_success_return($affiche_list);
    }
    /**
     * @name: xiaozou
     * @title: 消息详情
     * @url: 
     * @param {type} 
     * @return: 
     */
    public function get_affiches_order_info()
    {
        $affiche_id = input('affiche_id', '');
        $affiche_list = db('affiches_order')->find($affiche_id);
        $affiche_list['c_time'] = date('Y-m-d H:i:s', $affiche_list['c_time']);
        db('affiches_order')->where(['id' => $affiche_id])->update(['is_read' => 1]);
        json_success_return();
    }



    
}
